<template>
  <view>
    <view class="scroll-view-container">
      <scroll-view class="left-scroll-view" scroll-y :style="{height: wh+'px'}" >
        <view class="left-scroll-item" :class="{'active': active === index }" v-for="(item,index) in cateList" :key="index" @click="changActive(index)">
        {{item.cat_name}}
        </view>
      </scroll-view>
      <scroll-view scroll-y :style="{height: wh+'px'}" :scroll-top="scrollTop">
        <!-- 二级分类  -->
        <view class="cate-lv2-list" v-for="(item2,index2) in cate2" :key="index2">
          <view class="cate-lv2">
          {{item2.cat_name}}
          </view>
          <!-- 三级分类 -->
          <view class="cate-lv3-list">
            <navigator 
            class="cate-lv3" 
            :url="`/subpkg/goods_list/goods_list?cid=${item3.cat_id}`"
            v-for="(item3,index3) in item2.children" 
            :key="index3">
              <image :src="item3.cat_icon" mode=""></image>
              <text>{{item3.cat_name}}</text>
            </navigator>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'catePage',
  components: {},
  data() {
    return { 
      wh:0,
      active:0,
      cateList:[],
      cate2:[],
      scrollTop:0
    }; 
  },
  created() {},
  mounted() {},
  onLoad() {
    let {windowHeight} = uni.getSystemInfoSync()
    this.wh = windowHeight
    this.getCateList()
  }, 
  methods: {
    async getCateList(){
      let {data:{message,meta:{status}}}  = await uni.$http.get('/api/public/v1/categories')
      if(status!==200){
        return uni.$showMsg()
      }
      this.cateList = message
      this.cate2 = this.cateList[0].children
    },
    changActive(i){
      this.active = i
      this.cate2 = this.cateList[i].children
      this.scrollTop = this.scrollTop === 0 ? 1 : 0
    }
  } 
};
</script>

<style lang="scss">
  .scroll-view-container {
    display: flex;
  }
.left-scroll-view {
  width: 120px;
}  
.left-scroll-item {
    line-height: 60px;
    background-color: #f7f7f7;
    text-align: center;
    font-size: 12px;
    position: relative;
    &.active {
      background-color: #ffffff;
      &::before {
        content: ' ';
        display: block;
        width: 3px;
        height: 30px;
        background-color: #c00000;
        //相对于祖先当前有absolute,relative
        position: absolute;
        left: 0;
        top: 50%;
        transform: translateY(-50%);
      }
    }
  }
  .cate-lv2 {
    font-weight: bold;
    text-align: center;
    font-size: 12px;
    padding: 15px 0;
  }
  .cate-lv3-list {
    display: flex;
    flex-wrap: wrap;
  }
  .cate-lv3 {
    display: flex;
    flex-direction: column;
    justify-content: center;
    width: 33.33%;
    align-items: center;
    margin-bottom: 10px;
    
    image {
      width: 60px;
      height: 60px;
    }
    text {
      font-size: 12px;
    }
  }
</style>
